<?php

class CommonformAction extends BaseAction
{
    //人才招聘信息提交
    public function resume()
    {
        if (empty($_POST)) {
            $this->error("请勿恶意提交");
            return;
        }
        //主表多分表结构数据模型
        $data = $_POST;
        $data['time'] = time();
        $m = new ResumeModel($data['type']);
        $bool = $m->madd($data);

        if (!empty($bool)) {
            $this->error("投递简历失败！请稍后再试！");
            return false;
        } else {
            $rid = $m->getMainTableInsertId();
            $this->assign('waitSecond', '10');
            $this->success("投递简历成功！点击<a target='_blank' href='" . U('common/resumePrint?rid=' . $rid) . "'>打印</a>简历!", U('common/resume'));
            return true;
        }
    }

    //投票提交
    public function vote()
    {
        if (empty($_POST)) {
            $this->error("请勿恶意提交");
            return;
        }

        $data = $_POST;
        //限制投票
        $m2 = M('ipCheck');
        $map = array();
        $map['date'] = date('Y-m-d', time());
        $map['ip'] = get_client_ip();
        $map['fid'] = $data['id'];
        $check = $m2->where($map)->find();
        if (!empty($check)) {
            $this->error("您今天已经投过票了！");
            return;
        } else {
            $m2->create($map);
            $m2->add();
        }
        /*===========扩展信息保存开始============*/
        if (!empty($data['ext_remark'])) {
            $extM = M('Voteext');
            $extdata['fid'] = $data['id'];
            $extdata['remark'] = $data['ext_remark'];
            $extdata['ks'] = implode(" ", $data['ext_ks']);
            $extdata['date'] = date('Y-m-d', time());
            $extdata['time'] = time();
            $extM->create($extdata);
            $extM->add();
        }

        /*===========扩展信息保存结束============*/
        $itemsArr = array();
        foreach ($data as $k => $v) {
            if (strpos($k, "votea_") !== false) {
                $temp = array();
                $temp = explode("_", $v);
                $itemsArr[$temp[0]] = $temp[1];
            }
        }
        $m = M('Votepoll');
        $date = date('Y-m-d', time());
        if (!empty($itemsArr)) {
            foreach ($itemsArr as $k => $v) {
                $map = array();
                $map['date'] = array("eq", $date);
                $map['id'] = array('eq', $v);
                $check = $m->where($map)->find();
                //不存在
                if (empty($check)) {
                    $newDara = array();
                    $newDara['id'] = $v;
                    $newDara['fid'] = $k;
                    $newDara['date'] = $date;
                    $newDara['poll'] = 1;
                    $m->create($newDara);
                    $m->add();
                } else {
                    $m->where($map)->setInc("poll", 1);
                }
            }
        }

        if ($data['id'] == 1) {
            $this->success("提交问卷成功！", U('index/vote?id=' . $data['id']));
        } else if ($data['id'] == 2 || $data['id'] == 3) {
            $this->success("提交问卷成功！", U('index/survey?id=' . $data['id']));
        }
    }

    //搜索
    public function search()
    {
        import('ORG.Util.Page');
        $key = trim($this->_param("key"));
        $type = $this->_param("type");
        $type = !empty($type) ? $type : 1;
        //$key = iconv("gbk","utf-8",$key);
        if (empty($type)) {
            $this->error("搜索范围未指定！");
        }
        $rsArr = array();

        //主表模型查询
        if ($type == 1) {
            $m = M('archives');
            $map = array();
            $map['siteid'] = getSiteId();
            $map['title'] = array('like', "%" . $key . "%");
            $tpl = "search";
            $pagesize = 5;
            $this->assign("key", $key);
        } else if ($type == 2) {
            //更多类型查询逻辑
        }

        $count = $m->where($map)->count(); // 查询满足要求的总记录数
        $Page = new Page($count, $pagesize); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $rsArr = $m->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->select();

        $this->assign("rsArr", $rsArr);
        $this->assign("pageline", $show);
        $this->assign("count", $count);
        $theme = C('SYS_DEFAULT_THEME');
        $this->display($theme . ':' . $tpl);
    }

    //通用表单提交
    public function commonform()
    {
        $verify = $this->_param('verify');
        $type = $this->_param('type');
        if ($_SESSION['verify'] != md5($verify) && '-1' != $verify) {
            $this->error('验证码错误！');
            return;
        }

        if (empty($type)) {
            $this->error("表单类型出错！");
            return;
        }

        switch ($type) {
            case 1 :
                $table = "faq";
                break;
            case 2 :
                $table = "msg";
                break;
            default :
                $this->error("表单类型出错！");
                return;
        }

        $data = $_POST;
        //附加信息
        switch ($type) {
            case 1 :
                $data['time'] = time();
                $data['check'] = 2;
                $data['channel'] = 19;
                $data['siteid'] = getSiteId();
                break;
            case 2 :
                $table = "msg";
                break;
            default :
                break;
        }

        $m = M($table);
        $m->create($data);
        if ($m->add() !== false) {
            $this->success("您的信息已经成功提交，客服人员会及时联系您！");
            return;
        } else {
            $this->success("信息提交失败！请稍后再试！");
            return;
        }
    }
}
?>